'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 英雄技能
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var Code = require('../../../consts/code');
var moduleType = require('../../../consts/moduleType');
var operationType = require('../../../consts/operationType');
var skillService = require('../../../services/skillService');
var skillConf = require('../../../config/skillConf');
var constantUtil = require('../../../util/constantUtil');
var PlayerManager = require('../../../libs/playerManager');
var roleService = require('../../../services/roleService');
var taskService = require('../../../services/taskService');
var buildType = require('../../../consts/buildType');
var buildConf = require('../../../config/buildConf');
var skillModel = require('../../../models/skillModel.js');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

/**
 * 升级技能
 */
Handler.prototype.upgrade = function (msg, session, next) {
    var role_id = session.uid;
    var skill_type = msg.skill_type;
    if (!skill_type) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'skill', 'task', 'build', 'achieve', 'daily_task', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var role_model = player_manager.role;
                var skill_model = player_manager.skill;

                var skill = skill_model.getSkill(skill_type);
                if (!skill) {
                    return next(null, {
                        code: Code.SKILL_NOT_EXIST
                    });
                }

                // 获取技能等级配置
                var skill_lv_conf = skillConf.getSkillLvConf(skill_type, skill.getLv());
                var next_skill_lv_conf = skillConf.getSkillLvConf(skill_type, skill.getLv() + 1);
                if (!skill_lv_conf) {
                    return next(null, {
                        code: Code.SKILL_LV_CONF_ERROR
                    });
                }

                // 判断下级是否存在
                if (!next_skill_lv_conf) {
                    return next(null, {
                        code: Code.SKILL_LV_REACH_MAX
                    });
                }

                // 判断用户等级是否大于技能需要的等级
                var hero_college_build = player_manager.build.getBuildByType(buildType.HEROCOLLEGE);
                var hero_college_lv_conf = buildConf.getBuildLvConf(buildType.HEROCOLLEGE, hero_college_build.getLv());
                if (!hero_college_lv_conf || hero_college_lv_conf.skill_lvl_limit <= skill.getLv()) {
                    return next(null, {
                        code: Code.BUILD_HERO_COLLEGE_LV_NOT_ENOUGH
                    });
                }

                // 扣除金币
                if (!roleService.subGold(player_manager, moduleType.MDL_HERO_COLLAGE, operationType.OPR_HERO_COLLAGE_UP, skill_lv_conf.gold_cost)) {
                    return next(null, {
                        code: Code.USER_GOLD_NOT_ENOUGH
                    });
                }

                if (!skillService.upgradeSkill(player_manager, skill_type)) {
                    return next(null, {
                        code: Code.SKILL_NOT_EXIST
                    });
                }

                // 新增副本统计
                taskService.addUpgradeSkill(player_manager);

                // 刷新战力
                skillService.updateSkillPower(player_manager);

                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK
            });
        }
    );
};

/**
 * 选择技能
 * @param  {[type]}   msg     [description]
 * @param  {[type]}   session [description]
 * @param  {Function} next    [description]
 * @return {[type]}           [description]
 */
Handler.prototype.select = function (msg, session, next) {
    var role_id = session.uid;
    var select_skill_list = msg.select_skill_list || [];

    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(['skill', 'accrued'], cb);
            },
            function (cb) {
                var skill_model = player_manager.skill;

                if (!skill_model.setSelectSkillList(select_skill_list)) {
                    return next(null, {
                        code: Code.SKILL_SELECT_FAIL
                    });
                }

                // 刷新战力
                skillService.updateSkillPower(player_manager);

                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK
            });
        }
    );
};
